<template>
  <div>
    <el-upload
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :class="{ disable: fileList.length > 0 && single}"
      :on-preview="onPreview"
      :on-remove="onRemove"
      :on-change="onchange"
      :http-request="uploadImg"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>

    <el-dialog
      title="预览"
      :visible="isShowDialog"
      :append-to-body="true"
      @close="isShowDialog=false"
    >
      <el-row type="flex" justify="center">
        <img :src="srcURL" alt="" width="100%">
      </el-row>

    </el-dialog>

    <el-progress
      v-if="isShowPercent"
      :text-inside="true"
      :stroke-width="26"
      :percentage="percent"
      style="width:146px"
    />
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID4wYuznKbbLo1xcz5vTkkTv6zd1mHVTAa',
  SecretKey: 'A4h27vSigNoyrUqvjGXZKnzuYuznLdeP'
})
export default {
  props: {
    single: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      // fileList: [
      //   { url: 'https://hr-photos-1308572173.cos.ap-chengdu.myqcloud.com/22.jpg' }
      // ],
      uid: '', // 用于存储多张图片上存,避免覆盖第一张
      fileList: [],
      srcURL: '',
      isShowDialog: false,
      percent: 0,
      isShowPercent: false
    }
  },
  methods: {
    onPreview(file) {
      this.srcURL = file.url
      this.isShowDialog = true
    },
    onRemove(file, fileList) {
      this.fileList = fileList
      this.$message.success('删除成功')
    },
    onchange(file, fileList) {
      this.fileList = fileList
    },
    // 上传图片前校验
    beforeUpload(file) {
      this.uid = file.uid
      console.log(50, file)
      // 校验图片大小
      const size = 2 * 1024 * 1024
      if (file.size > size) {
        this.$message.warning('图片大小不能超过 2M')
        return false
      }
      // 校验格式
      const whileList = ['image/jpeg', 'image/png', 'image/jpg']
      if (!whileList.includes(file.type)) {
        this.$message.warning('图片格式只能是jpeg/png/jpg')
        return false
      }
    },
    uploadImg(params) {
      // 进度条:
      this.isShowPercent = true
      this.$message.success('上传')
      cos.putObject({
        // 储存桶名
        Bucket: 'hr-photos-1308572173',
        // 区域标志
        Region: 'ap-chengdu',
        // 文件名
        Key: params.file.name,
        // 固定写法
        StorageClass: 'STANDARD',
        Body: params.file,
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          this.percent = Math.round(progressData.percent * 100)
        }
      }, (err, data) => {
        console.log(err || data)
        const img = this.fileList.find(item => item.uid === this.uid)
        img.url = 'http://' + data.Location
        img.status = 'success'
        setTimeout(() => {
          this.isShowPercent = false
        }, 200)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.disable {
  ::v-deep .el-upload--picture-card {
    display: none;
  }
}
</style>
